package com.ys.erp.stage_mall_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ys.erp.entity.data.bean.GoodsSubscribe;
import com.ys.erp.entity.service.IGoodsSubscribeService;
import com.ys.erp.stage_mall_service.utils.WxSendMessageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lwj
 * @date 2025/5/28
 */
@Service
@RequiredArgsConstructor
@Slf4j
@RefreshScope
public class GoodsSubscribeService {

    private final IGoodsSubscribeService iGoodsSubscribeService;

    private final WxSendMessageUtil wxSendMessageUtil;

    @Async
    public void asyncSendMessage(List<Long> specsIds) {
        if (specsIds.isEmpty()) {
            return;
        }
        QueryWrapper<GoodsSubscribe> specsQw = new QueryWrapper<>();
        specsQw.lambda().in(GoodsSubscribe::getSpecsId, specsIds);
        List<GoodsSubscribe> goodsSubscribes = iGoodsSubscribeService.list(specsQw);
        if (goodsSubscribes.isEmpty()) {
            return;
        }
        for (GoodsSubscribe goodsSubscribe: goodsSubscribes) {
            wxSendMessageUtil.subscribeGoods(goodsSubscribe.getUserId(), goodsSubscribe.getGoodsName());
        }
        iGoodsSubscribeService.remove(specsQw);
    }


}
